<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class GoodsDetailController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getAdd(Request $request)
    {	$id = $request -> input('id');
        return view('admin.goodsdetail.add',['id'=>$id]);
    }

    public function postDoadd(Request $request)
    {
    	$detaildata = $request->except('_token');

    	if(!empty($detaildata['pic1'])){
        //获取上传文件的后缀
        $ext = $request -> file('pic1') -> getClientOriginalExtension();
        //随机生成一个文件前缀名字
        $filename = date('Ymd',time()).mt_rand(10000,99999);
        //前缀后缀拼接成最后新的文件名称
        $newfilename1 = 'pic1_'.$filename.'.'.$ext;
        //上传文件到指定目录
        $request -> file('pic1') -> move('home_assoc/images/',$newfilename1);
	    }else{
	    	$newfilename1 = 'default.jpg';
	    }
	    if(!empty($detaildata['pic2'])){
	        //获取上传文件的后缀
	        $ext = $request -> file('pic2') -> getClientOriginalExtension();
	        //随机生成一个文件前缀名字
	        $filename = date('Ymd',time()).mt_rand(10000,99999);
	        //前缀后缀拼接成最后新的文件名称
	        $newfilename2 = 'pic2_'.$filename.'.'.$ext;
	        //上传文件到指定目录
	        $request -> file('pic2') -> move('home_assoc/images/',$newfilename2);
	    }else{
	    	$newfilename2 = 'default.jpg';
	    }
	    if(!empty($detaildata['pic3'])){
	        //获取上传文件的后缀
	        $ext = $request -> file('pic3') -> getClientOriginalExtension();
	        //随机生成一个文件前缀名字
	        $filename = date('Ymd',time()).mt_rand(10000,99999);
	        //前缀后缀拼接成最后新的文件名称
	        $newfilename3 = 'pic3_'.$filename.'.'.$ext;
	        //上传文件到指定目录
	        $request -> file('pic3') -> move('home_assoc/images/',$newfilename3);
	    }else{
	    	$newfilename3 = 'default.jpg';
	    }

	    $detaildata['pic1'] = $newfilename1;
	    $detaildata['pic2'] = $newfilename2;
	    $detaildata['pic3'] = $newfilename3;
	    $detaildata['goodstime'] = date('Y-m-d H:i:s',time());
       

	    $res = DB::table('tbl_goodsparam')->insert($detaildata);

	     if($res){
            return redirect('/admin/goodsdetail/list')->with('success','添加成功');
       	}else{
            return back()->with('error','添加失败');
       	}
	}

	public function getList(Request $request){
		$count = $request ->input('count',10);
        $search = $request->input('search','');

        $data = DB::table('tbl_goodsparam') -> where('gid','like','%'.$search.'%') ->paginate($count);

		return view('admin.goodsdetail.list',['data'=>$data,'request'=>$request->all()]);
	}

	public function getDelete($id){
		 $res = DB::table('tbl_goodsparam')->where('id',$id)->delete();
         if($res){
             return redirect('/admin/goodsdetail/list')->with('success','删除成功');
         }else{
             return redirect('/admin/goodsdetail/list')->with('error','删除失败');
         }
	}

	public function getEdit($id){
      
       $data = DB::table('tbl_goodsparam') -> where('id',$id)->first();
       return view('admin.goodsdetail.update',['data'=>$data]);
    }
    //修改信息更改控制
    public function postUpdate(Request $request)
    {
        $detaildata = $request->except('_token');
        // dd($detaildata);
        if(!empty($detaildata['pic1'])){
        //获取上传文件的后缀
        $ext = $request -> file('pic1') -> getClientOriginalExtension();
        //随机生成一个文件前缀名字
        $filename = date('Ymd',time()).mt_rand(10000,99999);
        //前缀后缀拼接成最后新的文件名称
        $newfilename1 = 'pic1_'.$filename.'.'.$ext;
        //上传文件到指定目录
        $request -> file('pic1') -> move('home_assoc/images/',$newfilename1);
        }else{
           $data = DB::table('tbl_goodsparam')->where('id',$detaildata['id'])->first();
            $newfilename1 = $data['pic1'];
        }
        if(!empty($detaildata['pic2'])){
            //获取上传文件的后缀
            $ext = $request -> file('pic2') -> getClientOriginalExtension();
            //随机生成一个文件前缀名字
            $filename = date('Ymd',time()).mt_rand(10000,99999);
            //前缀后缀拼接成最后新的文件名称
            $newfilename2 = 'pic2_'.$filename.'.'.$ext;
            //上传文件到指定目录
            $request -> file('pic2') -> move('home_assoc/images/',$newfilename2);
        }else{
            $data = DB::table('tbl_goodsparam')->where('id',$detaildata['id'])->first();
            $newfilename2 = $data['pic2'];
        }
        if(!empty($detaildata['pic3'])){
            //获取上传文件的后缀
            $ext = $request -> file('pic3') -> getClientOriginalExtension();
            //随机生成一个文件前缀名字
            $filename = date('Ymd',time()).mt_rand(10000,99999);
            //前缀后缀拼接成最后新的文件名称
            $newfilename3 = 'pic3_'.$filename.'.'.$ext;
            //上传文件到指定目录
            $request -> file('pic3') -> move('home_assoc/images/',$newfilename3);
        }else{
            $data = DB::table('tbl_goodsparam')->where('id',$detaildata['id'])->first();
            $newfilename3 = $data['pic3'];
        }
        $detaildata['pic1'] = $newfilename1;
        $detaildata['pic2'] = $newfilename2;
        $detaildata['pic3'] = $newfilename3;
        $detaildata['goodstime'] = date('Y-m-d H:i:s',time());
        array_shift($detaildata);
        $res = DB::table('tbl_goodsparam')->update($detaildata);

         if($res){
            return redirect('/admin/goodsdetail/list')->with('success','更新成功');
        }else{
            return back()->with('error','更新失败');
        }
    }


}
